package C8_13_ReplaceTypeCodeWithClass;

import org.junit.Test;

/**
 * @author xufy
 * @version 1.0
 * @description 以类取代类型码
 **/
public class ReplaceTypeCodeWithClass {

    @Test
    public void test1() {
        Person2 person2 = new Person2(Person2.O);
        person2.setBloodGroup(Person2.A);
        System.out.println("重构前： " + person2.getBloodGroup());
    }

    /**
     * 重构后
     */
    @Test
    public void test2() {
        Person person = new Person(BloodGroup.O.getCode());
        person.setBloodGroup(BloodGroup.A.getCode());
        System.out.println("重构后： " + person.getBloodGroup());
    }


}

/**
 * 重构前
 */
class Person2 {
    public static final int O = 0;
    public static final int A = 1;
    public static final int B = 2;
    public static final int AB = 3;

    private int _bloodGroup;

    public Person2(int bloodGroup) {

        _bloodGroup = bloodGroup;
    }

    public void setBloodGroup(int arg) {
        _bloodGroup = arg;
    }

    public int getBloodGroup() {
        return _bloodGroup;
    }

}

class BloodGroup {
    public static final BloodGroup O = new BloodGroup(0);
    public static final BloodGroup A = new BloodGroup(1);
    public static final BloodGroup B = new BloodGroup(2);
    public static final BloodGroup AB = new BloodGroup(3);
    private static final BloodGroup[] _values = {O, A, B, AB};

    private final int _code;

    private BloodGroup(int code) {
        _code = code;
    }

    public int getCode() {
        return _code;
    }

    public static BloodGroup code(int arg) {
        return _values[arg];
    }

}
class Person{
    public static final int O=BloodGroup.O.getCode();
    public static final int A=BloodGroup.A.getCode();
    public static final int B=BloodGroup.B.getCode();
    public static final int AB=BloodGroup.AB.getCode();

    private BloodGroup _bloodGroup;
    public Person(int bloodGroup){
        _bloodGroup=BloodGroup.code(bloodGroup);
    }
    public int getBloodGroup(){
        return _bloodGroup.getCode();
    }
    public void setBloodGroup(int arg){
        _bloodGroup=BloodGroup.code(arg);
    }

}